﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Shape.Mvp.Sample.Views
{
   public partial class ExerciseListForm : Form, IExerciseListView
   {
   #region Events
      public event Action<string> ShowExerciseDetails;
   #endregion
      
      public ExerciseListForm()
      {
         InitializeComponent();
      }

      public void AddExercise(string key, string name, string startingPosition)
      {
         lvwExercises.Items.Add(
            new ListViewItem() { Name = key, Text = name });
      }

      public void UpdateExercise(string key, string name)
      {
         var items = lvwExercises.Items.Find(key, searchAllSubItems:false);

         if (items.Any())
         {
				try 
				{	        
		    		lvwExercises.BeginUpdate();
					items.First().Text = name;
				}
				finally
				{
					lvwExercises.EndUpdate();
				}
         }
      }

      private void OnListViewDoubleClick(object sender, EventArgs e)
      {
         if (lvwExercises.SelectedItems.Count > 0)
         {
            if (ShowExerciseDetails != null)
            {
               ShowExerciseDetails(lvwExercises.SelectedItems[0].Name);
            }
            
         }
      }

   }
}
